﻿#include "dialog.h"
#include "ui_dialog.h"
#include <QSettings>
#include <QFile>
#include <QTextStream>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    connect(&m_dp1, SIGNAL(dataReady(double,double)), this, SLOT(on_updateData1(double,double)));
    connect(&m_dp2, SIGNAL(dataReady(double,double)), this, SLOT(on_updateData2(double,double)));
    connect(&m_dp3, SIGNAL(dataReady(double,double)), this, SLOT(on_updateData3(double,double)));
    connect(&m_dp4, SIGNAL(dataReady(double,double)), this, SLOT(on_updateData4(double,double)));
    connect(&m_dp5, SIGNAL(dataReady(double,double)), this, SLOT(on_updateData5(double,double)));

    connect(ui->pushButton, SIGNAL(clicked(bool)),this, SLOT(on_button()));

    m_timer.setInterval(2000);
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(on_timer()));
    connect(&m_timerLog, SIGNAL(timeout()), this, SLOT(on_log()));
}

void Dialog::on_log()
{
    QDateTime t = QDateTime::currentDateTime();
    QString date = t.toString("yyyy-MM-dd");
    QString time = t.toString("yy-MM-dd hh:mm:ss");
    QString fileName = QCoreApplication::applicationDirPath() + "/" + date + ".txt";
    QFile file(fileName);
    if( file.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append) )
    {
        QTextStream out(&file);
        out << time << ", ";
        out << m_valueX[0] << ", " << m_valueY[0] << ", ";
        out << m_valueX[1] << ", " << m_valueY[1] << ", ";
        out << m_valueX[2] << ", " << m_valueY[2] << ", ";
        out << m_valueX[3] << ", " << m_valueY[3] << ", ";
        out << m_valueX[4] << ", " << m_valueY[4] << endl;
    }
}

void Dialog::initPort()
{
    QString fileName = QCoreApplication::applicationDirPath() + "/settings.ini";
    QSettings settings(fileName, QSettings::IniFormat);
    QString port1 = settings.value("SerialPort/Port1", "COM3").toString();
    QString port2 = settings.value("SerialPort/Port2", "COM4").toString();
    QString port3 = settings.value("SerialPort/Port3", "COM6").toString();
    QString port4 = settings.value("SerialPort/Port4", "COM7").toString();
    QString port5 = settings.value("SerialPort/Port5", "COM8").toString();

    m_dp1.setPortName(port1);
    m_dp2.setPortName(port2);
    m_dp3.setPortName(port3);
    m_dp4.setPortName(port4);
    m_dp5.setPortName(port5);

    m_dp1.start();
    m_dp2.start();
    m_dp3.start();
    m_dp4.start();
    m_dp5.start();
}

void Dialog::on_button()
{
    QString text = ui->pushButton->text();
    if(text == "Start")
    {
        startMonitor();
        ui->pushButton->setText("Stop");

        if(ui->checkBox->isChecked())
        {
            int interval  = ui->spinBoxInterval->value();
            m_timerLog.setInterval(interval * 1000);
            m_timerLog.start();
        }
    }
    else
    {
        stopMonitor();
        ui->pushButton->setText("Start");
        m_timerLog.stop();
    }
}

void Dialog::stopMonitor()
{
    m_dp1.stop();
    m_dp2.stop();
    m_dp3.stop();
    m_dp4.stop();
    m_dp5.stop();
    m_timer.stop();
}

void Dialog::startMonitor()
{
    m_time[0] = QDateTime::currentDateTime();
    m_time[1] = QDateTime::currentDateTime();
    m_time[2] = QDateTime::currentDateTime();
    m_time[3] = QDateTime::currentDateTime();
    m_time[4] = QDateTime::currentDateTime();
    initPort();
    m_timer.start();
}

void Dialog::on_timer()
{
    QDateTime t = QDateTime::currentDateTime();
    if(m_time[0].secsTo(t) > 1)
    {
        m_valueX[0] = NAN;
        m_valueY[0] = NAN;
        ui->lcdX_1->display("Err");
        ui->lcdY_1->display("Err");
    }
    if(m_time[1].secsTo(t) > 1)
    {
        m_valueX[1] = NAN;
        m_valueY[1] = NAN;
        ui->lcdX_2->display("Err");
        ui->lcdY_2->display("Err");
    }
    if(m_time[2].secsTo(t) > 1)
    {
        m_valueX[2] = NAN;
        m_valueY[2] = NAN;
        ui->lcdX_3->display("Err");
        ui->lcdY_3->display("Err");
    }
    if(m_time[3].secsTo(t) > 1)
    {
        m_valueX[3] = NAN;
        m_valueY[3] = NAN;
        ui->lcdX_4->display("Err");
        ui->lcdY_4->display("Err");
    }
    if(m_time[4].secsTo(t) > 1)
    {
        m_valueX[4] = NAN;
        m_valueY[4] = NAN;
        ui->lcdX_5->display("Err");
        ui->lcdY_5->display("Err");
    }
}
void Dialog::on_updateData1(double x, double y)
{
    m_time[0] = QDateTime::currentDateTime();
    m_valueX[0] = x;
    m_valueY[0] = y;
    ui->lcdX_1->display(x);
    ui->lcdY_1->display(y);
}

void Dialog::on_updateData2(double x, double y)
{
    m_time[1] = QDateTime::currentDateTime();
    m_valueX[1] = x;
    m_valueY[1] = y;
    ui->lcdX_2->display(x);
    ui->lcdY_2->display(y);
}

void Dialog::on_updateData3(double x, double y)
{
    m_time[2] = QDateTime::currentDateTime();
    m_valueX[2] = x;
    m_valueY[2] = y;
    ui->lcdX_3->display(x);
    ui->lcdY_3->display(y);
}

void Dialog::on_updateData4(double x, double y)
{
    m_time[3] = QDateTime::currentDateTime();
    m_valueX[3] = x;
    m_valueY[3] = y;
    ui->lcdX_4->display(x);
    ui->lcdY_4->display(y);
}


void Dialog::on_updateData5(double x, double y)
{
    m_time[4] = QDateTime::currentDateTime();
    m_valueX[4] = x;
    m_valueY[4] = y;
    ui->lcdX_5->display(x);
    ui->lcdY_5->display(y);
}
Dialog::~Dialog()
{
    delete ui;
}
